<?php
namespace App\Services;

use CodeIgniter\Model;

class odds {

    // 分差倍数
    public $cs = 5;
    // 录取概率初始值 百分比
    public $base_rate = 30;
    //
    public $feed_value = 15;

    // 分差高分区限制百分比
    public $up_percent = 30;
    // 中分区限制百分比
    public $mid_percent = 45;
    // 低分区限制百分比
    public $down_percent = 55;

    // 录取概率类型(C:冲击,W:稳妥, B:保底)
    public $odds_type = '';

    // 冲击
    public $odds_cj_rate = 1;
    // 低分(稳妥)区比例值
    public $odds_wt_rate = 1.2;
    // 低分(保底)区比例值
    public $odds_bd_rate = 1.4;

    // 当前考生分差值
    public $Avalue = 0;

    // 当前学校所在分差
    public $schAvalue = 0;


    public $average = 0;
    // 当前位次
    public $Asection = 0;
    // 位次
    public $schSection = 0;
    //
    public $schAverage = 0;

    public $spSectionData;

    //
    protected $range_rate = [
        'C' => ['range' => [0,15],'rate' => 0.45],
        'W' => ['range' => [0,-15],'rate' => 0.45],
        'B' => ['range' => [-15,-30], 'rate' => 0.75]
    ];

    public function __construct() {}

    // 比较3年平均分差
    function compares_avalue(){
        // 高分区
        if ( ($this->schAvalue - $this->Avalue) >= 0 ) {
            $u = round((($this->schAvalue - $this->Avalue) / $this->Avalue) * 100);
            $odds =  ($u >= $this->up_percent) ? -($this->up_percent - 1) : -$u;
            $this->odds_type = $this->odds_type ?:(($odds + $this->base_rate) < $this->feed_value ? 'N':'C');
            $bs = $this->base_rate;
        } else {
            // 低分区
            $b = ($this->Avalue - $this->schAvalue) <= $this->mid_percent ? $this->mid_percent: $this->down_percent;
            $d = (($this->Avalue - $this->schAvalue) / $this->Avalue) * 100;
            $down_rate = $d >= $this->mid_percent ? $b : $d;
            $odds = $down_rate ; $bs = $this->base_rate + $this->feed_value ;
            $this->odds_type = $this->odds_type?:($b == $this->mid_percent ? 'W' : 'B');
        }

        // 不同区域类型不同权重比例
        $rate = ($this->odds_type == 'W' ? $this->odds_wt_rate : ($this->odds_type == 'B'?$this->odds_bd_rate:$this->odds_cj_rate));
        //
        $odds = $bs * $rate  + round($odds);
        //
        $AB = (intval($odds / $this->cs) * $this->cs);
        //
        return (($odds > $this->cs) && $odds > 20) ? ($AB > 99 ? 99: $AB) : $odds;
    }

    //  近3年位次比较
    function compares_asection(){
        $ra = $this->range_rate[$this->odds_type];
        $range = $ra['range'];  $rate = $ra['rate']; $odds = 0; $feed = 0;
        // 冲击
        if ($this->odds_type == 'C') {
            $b = $range[1] - $range[0];
            $temp_odds = (abs($this->schAverage - $this->average) / $b) * $rate;
            $odds = ($temp_odds >= $rate) ? 1 : ($rate - $temp_odds);
        }

        // 稳妥
        if ($this->odds_type == 'W') {
            $b = $range[0] - $range[1];
            $temp_odds = (abs($this->schAverage - ($this->average)) / $b) * $rate;
            $odds = ($temp_odds + $rate)>0.79 ? 0.79 : ($rate + $temp_odds);
        }

        // 保底
        if ($this->odds_type == 'B') {
            $b = $range[0] - $range[1];
            $temp_odds = (abs($this->schAverage - ($this->average - abs($range[0]))) / $b) * $rate;
            $odds = ($temp_odds + $rate) >= 1 ? 0.99 : ($rate + $temp_odds);
        }
        $odds = round($odds < 1 ? ($odds * 100) : $odds);
        return $odds;//(($odds > $this->cs) && $odds > 20) ? ($a > 99 ? 99: $a) : $odds;
    }

    // 专业位次比较
    public function special_section(){
        // 位次及分数
        $odds = 0; $s = $this->spSectionData;

        if ( !$s || !$s['num'] ) return 0;
        // 专业位次
        $section = $s['avg_section'] ;
        // 专业平均分
        $score = $s['avg_score'];

        // 考生位次
        $self_section = $this->Asection;
        // 考生分数
        $self_score = $this->average;
        //
        $range = 15; $rate = 0.45;
        // 存在某一年情况
        if ( $s['num'] == 1 ) {
            $section = $s['total_section']; $score = $s['total_score'];
        }

        if ($s['num'] == 2) {
            $year_bl = [0.85,0.15]; $scn = $sc = 0;
            foreach ( $s['arr_section'] as $k=>$v ) {
                $scn += $year_bl[$k] * $v; $sc += $year_bl[$k] * $s['arr_score'][$k];
            }
            $section = $scn; $score = $sc;
        }

        // 考生分数与专业分数差值
        $avalue = abs($self_score - $score); $temp_odds = ($avalue / $range) * $rate;

        // 考生位次小于专业位次 (冲击)
        if ( $self_section > $section ) {
            // 存在考生分数大于专业分数情况
            if ( $self_score > $score ) {
                // $avalue = ($avalue - ($self_score - $score))?:($range / 100);
                $temp_odds = (1 - $section / $self_section);
            }
            // log_message('error',"section:".$temp_odds);
            $odds = ( $temp_odds >= $rate ) ? 1 : ( $rate - $temp_odds);
        }

        // 考生位次大于专业位次 (稳妥及保底)
        if ( $self_section < $section ){
            if ( $avalue <= $range ) {
                $odds = ($temp_odds + $rate)>0.74 ? 0.74 : ($rate + $temp_odds);
            }

            if ( $avalue > $range ) {
                $rate = 0.75;
                $temp_odds = ($avalue / $range) * $rate;
                $odds = ($temp_odds + $rate) >= 1 ? 0.99 : ($rate + $temp_odds);
            }
        }
        $odds = round($odds < 1 ? ($odds * 100) : $odds);

        return $odds;
    }
}